using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using PluginHandler.UI;

namespace SqlPlugin.UI
{
    internal partial class ConnectionDialog : UserControl, IConnectionDialog
    {
        private DbConnectionStringBuilder _connectionStringBuilder;

        public ConnectionDialog()
        {
            InitializeComponent();
        }

        #region IConnectionDialog Member

        public DbConnectionStringBuilder ConnectionStringDetails
        {
            get 
            {
                if (_connectionStringBuilder == null)
                    _connectionStringBuilder = new DbConnectionStringBuilder();

                _connectionStringBuilder["Data Source"] = txtServer.Text;
                _connectionStringBuilder["Initial Catalog"] = txtDatabase.Text;
                _connectionStringBuilder["Integrated Security"] = cmbAuthentication.SelectedIndex == 0 ? "SSPI" : "";
                _connectionStringBuilder["User Id"] = txtUsername.Text;
                _connectionStringBuilder["Password"] = txtPassword.Text;

                return _connectionStringBuilder;
            }
            set 
            {
                _connectionStringBuilder = value;
                
                txtServer.Text = TryGetValue(_connectionStringBuilder, "Data Source");
                txtDatabase.Text = TryGetValue(_connectionStringBuilder, "Initial Catalog");
                cmbAuthentication.SelectedIndex = _connectionStringBuilder.ContainsKey("Integrated Security") ? 0 : 1;
                txtUsername.Text = TryGetValue(_connectionStringBuilder, "User Id");
                txtPassword.Text = TryGetValue(_connectionStringBuilder, "Password");
            }
        }

        public bool FilledComplete
        {
            get
            {
                bool filledComplete = false;

                filledComplete = 
                    txtServer.Text.Trim().Length > 0 &&
                    txtDatabase.Text.Trim().Length > 0;

                if (filledComplete && cmbAuthentication.SelectedIndex == 1)
                {
                    filledComplete = txtUsername.Text.Trim().Length > 0;
                }
                
                return filledComplete;
            }
        }

        public bool AskForPassword
        {
            get { return chkAskForPassword.Checked; }
            set { chkAskForPassword.Checked = value; }
        }
        #endregion

        private void cmbAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbAuthentication.SelectedIndex == 0)
            {
                pnlSQLAuthentication.Enabled = false;
            }
            else
            {
                pnlSQLAuthentication.Enabled = true;
            }
        }

        private void ConnectionDialog_Load(object sender, EventArgs e)
        {
            cmbAuthentication.SelectedIndex = 0;
        }

        private string TryGetValue(DbConnectionStringBuilder dbConnectionStringBuilder, string keyword)
        {
            if (dbConnectionStringBuilder.ContainsKey(keyword))
            {
                return dbConnectionStringBuilder[keyword].ToString();
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
